using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QuanLyThuoc.DAL;
namespace QuanLyThuoc.Class
{
   public class PublicClass
    {
        private static KetNoiDAL con = new KetNoiDAL();
       
        public static string GetNewStringID(string pcTableName, string pcKeyColumn, int pnLen, string pcPrefix)
        {
            string cReturn = "", lcPrefix = "";
            if (pcPrefix == "YYMM")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0');
            else if (pcPrefix == "MMYY")
                lcPrefix = DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2);
            else if (pcPrefix == "YYMMDD")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');
            else if (pcPrefix == "DDMMYY")
                lcPrefix = DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2);
            else if (pcPrefix == "YY")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2);
            else if (pcPrefix == "YYYY")
                lcPrefix = DateTime.Now.Year.ToString();
            else if (pcPrefix == "YYMM-")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + "-";
            else if (pcPrefix == "MMYY-")
                lcPrefix = DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2) + "-";
            else if (pcPrefix == "YYMMDD-")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + "-";
            else if (pcPrefix == "DDMMYY-")
                lcPrefix = DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2) + "-";
            else if (pcPrefix == "YY-")
                lcPrefix = DateTime.Now.Year.ToString().Substring(2, 2) + "-";
            else if (pcPrefix == "YYYY-")
                lcPrefix = DateTime.Now.Year.ToString() + "-";
            else
                lcPrefix = pcPrefix;

            try
            {
                string lcQuery = "SELECT TOP 1 " + pcKeyColumn + " FROM " + pcTableName;
                if (!string.IsNullOrEmpty(pcPrefix))
                    lcQuery += " WHERE LEFT(" + pcKeyColumn + ", " + lcPrefix.Trim().Length.ToString() + ") = '" + lcPrefix.Trim() + "'";
                lcQuery += " ORDER BY " + pcKeyColumn + " DESC";
               // DataTable dtNewID = Global.dbCon.GetTable(lcQuery, "dtNewID");
                DataTable dtNewID = con.GetDataTables(lcQuery, "dtNewID");
                if (dtNewID.Rows.Count > 0)
                    cReturn = dtNewID.Rows[0][0].ToString().Trim().PadLeft(1, '0');
                if (string.IsNullOrEmpty(cReturn.Trim()))
                    cReturn = lcPrefix + "1".PadLeft(pnLen, '0');
                else
                    cReturn = con.TangChuoi(cReturn);
            }
            catch { }
            return cReturn;
        }
       public static bool CheckExistKeyString(string pcTableName, string pcKeyColumn, string pcKeyValue)
       {
           string lcQuery = "SELECT TOP 1 " + pcKeyColumn + " FROM " + pcTableName + " WHERE " + pcKeyColumn + " = '" + pcKeyValue.Trim() + "'";
           DataTable dtCheck = con.GetTables(lcQuery, "dtCheck");
           return dtCheck.Rows.Count > 0;
       }
    }
}
